<template>
   <div class="flower-type-container">
      <el-card class="header">
        <div class="header-content">
            <div class="search">
                <!-- <span>鲜花种类添加</span>
                <el-input v-model="flowerTypeValue" class="input"></el-input>
                <el-button @click="flowerTypeAdd" type="primary">添加</el-button> -->
                <el-tag  type="primary" size="large">您可直接点击下方的删除按钮，删除此种类的全部鲜花</el-tag>
            </div>
        </div>
      </el-card>
      <el-card>
        <el-table :data="tableData" style="width: 100%" border >
          <!-- 序列号 -->
          <el-table-column label="#" type="index" align="center" />
          <!-- 鲜花名称 -->
          <el-table-column label="鲜花种类名称" prop="type" align="center"/>
          <!-- 操作 -->
          <el-table-column
            :label="$t('msg.excel.action')"
            fixed="right"
            width="180"
            align="center"
          >
            <template #default="{ row }">
              <el-button type="danger" size="mini" @click="onDeleteFlowerType(row)">{{
                $t('msg.excel.remove')
              }}</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          class="pagination"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :current-page="page"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="size"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-card>
    </div>
</template>

<script setup>
import {ref,reactive,onMounted} from 'vue'
import {flowerType,flowerTypeDelete} from '@/api/flowers'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useI18n } from 'vue-i18n'

// table值初始化
const tableData = ref([])
// 获取i18n
const i18n = useI18n()
// 获取鲜花种类并且赋值的函数
const getFlowerType = async() =>{
    // 获取全部种类
    const data = await flowerType()
    tableData.value = []
    // 对table进行赋值
    data.typeData.forEach(item =>{
        tableData.value.push({type:item})
    })
}
// 删除鲜花种类的函数
const onDeleteFlowerType = async (row) =>{
    // await flowerTypeDelete({type:row.type})
    ElMessageBox.confirm(
    `确认要删除${row.type}的种类吗，该种类的所有鲜花信息会被全部删除哦`,
      '温馨提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }
    ).then(async () => {
      await flowerTypeDelete({type:row.type})
      ElMessage({
        type: 'success',
        message: i18n.t('msg.excel.removeSuccess')
      })
      getFlowerType()
    })
}
onMounted(async()=>{
    getFlowerType()
})
</script>

<style lang="scss" scoped>
.flower-type-container {
    .header {
      margin-bottom: 22px;
      text-align: right;
      .header-content{
        display: flex;
        justify-content: space-between;
        align-items: center;
        .search{
            display: flex;
            align-items: center;
            width: 50%;
            .input{
                width: 30%;
                margin: 0 10px;
            }
        }
      }
    }
    :deep(.el-image) {
      width: 60px;
      height: 60px;
      border-radius: 50%;
    }
  
    :deep(.el-tag) {
      margin-right: 6px;
    }
  
    .pagination {
      margin-top: 20px;
      text-align: center;
    }
  }
</style>